package com.probiz.estoresa.customer.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.attribute.AttributeConstants;
import com.probiz.estore.attribute.service.AttributeManager;
import com.probiz.estore.common.model.attribute.Attribute;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.webapp.util.RequestUtil;

public class CustomerAttributeController extends GenericStoreAdminController<Attribute> {
    private AttributeManager attributeManager = null;

	public void setAttributeManager(AttributeManager attributeManager) {
		this.attributeManager = attributeManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(Attribute attribute) {
		return attribute.getAttributeName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = attributeManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#preSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, Attribute entity, BindException errors) {
	}
	@Override
	public ModelAndView search(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/customerAttributeList");
		List<Attribute> attributeList = attributeManager.getAttributes(AttributeConstants.MODULE_CUSTOMER.shortValue());
		mv.addObject("attributeList", attributeList);
		return mv;
	}
	
	public ModelAndView attributeDefine(HttpServletRequest request,	HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("customer/customerAttributeForm");
		mv.addObject("attributeId", RequestUtil.getParameterNullSafe(request,"attributeId"));
		return mv;
	}
	/**
	 * 更新排序值
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView updateSortOrders(HttpServletRequest request,HttpServletResponse response) throws Exception {
		// 取得要删除的记录的id的列表。注意：选择框的id要是"multiIds"。
		String[] ids = request.getParameterValues("multiIds2");
		if (ids != null && ids.length > 0) {
			for(int i=0; i<ids.length; i++){
				Attribute attribute = attributeManager.getById(Integer.valueOf(ids[i]));
				attribute.setSortOrder(i);
				attributeManager.save(attribute);
			}
		}
		return this.getModelAndAjaxJsonView(null);
	}
	@Override
	public ModelAndView multiDelete(HttpServletRequest request,
			HttpServletResponse response) {
		// 取得要删除的记录的id的列表。注意：选择框的id要是"multiIds"。
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			try{
				mgr.deleteAllByIds(ids);
			}catch (DataIntegrityViolationException e) {
				//Hibernate底层仍会log此出错误信息
				this.saveErrorMessage(request, this.getMessage("common.message.delete.constraintViolation"));
				return this.returnToSearch(request, response);
			}
			String message = getMessage("customerAttribute.deleted.multi", new Object[] {ids.length });
			saveMessage(request, message);
		}
		return this.getRedirectView("/customer/customerAttribute.html");
	}
}
